﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Net6Shell.Vo;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Dto;
using ReportDesignerServer.IRepository;
using ReportDesignerServer.Repository;
using SqlSugar;

namespace ReportDesignerServer.Controllers
{

    /// <summary>
    /// RtmsRecordController
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RtmsRecordController : ControllerBase
    {
        IRtmsRecordRepository rtmsRecordRepository;

        public RtmsRecordController(IRtmsRecordRepository rtmsRecordRepository)
        {
            this.rtmsRecordRepository = rtmsRecordRepository;
        }


        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt QueryRtmsRecord([FromBody] RtmsQueryModel query)
        {
            Expressionable<RtmsRecord> exp = Expressionable.Create<RtmsRecord>();
            if (query.StartTime != null && query.EndTime != null)
            {
                exp.And(it => it.RECORD_TIME >= ((DateTime)query.StartTime).Date);
                exp.And(it => it.RECORD_TIME < ((DateTime)query.EndTime).Date.AddDays(1));
            }
            else
                return Rt.Fail("请选择时间段！");

            if (!string.IsNullOrEmpty(query.SearchTest))
            {
                exp.And(it => it.NAME.Contains(query.SearchTest) || it.BED.Contains(query.SearchTest));
            }

            var list = rtmsRecordRepository.GetListByWhere(exp);
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].SID = list[i].ID.ToString();
                }
            }
            return Rt.SuccessObj(list);
        }


        /// <summary>
        /// 通过id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Rt GetRtmsRecordById(string id)
        {
            long lid = 0;
            if (long.TryParse(id, out lid))
            {
                Expressionable<RtmsRecord> exp = Expressionable.Create<RtmsRecord>();
                exp.And(it => it.ID == lid);
                var model = rtmsRecordRepository.GetListByWhere(exp).FirstOrDefault();
                model.SID = model.ID.ToString();
                return Rt.SuccessObj(model);
            }
            else
            {
                return Rt.Error();
            }
        }

        /// <summary>
        /// 保存一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveRtmsRecord([FromBody] RtmsRecord model)
        {
            long res = 0;
            if (model != null)
            {
                long lid = 0;
                if (long.TryParse(model.SID, out lid))
                {
                    model.ID = lid;
                }
                if (model.ID == 0)
                {
                    res = rtmsRecordRepository.ExecuteReturnSnowflakeId(model);
                }
                else
                {
                    res = rtmsRecordRepository.Update(model);
                }
            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }



        /// <summary>
        /// 添加多条经颅磁数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveRtmsRecords([FromBody] List<RtmsRecord> model)
        {
            List<long> ids=new List<long> ();
            if (model != null)
            {
                 ids = rtmsRecordRepository.ExecuteRecordsSnowflakeId(model);
            }
            if (ids!=null && ids?.Count > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt DelRtmsRecord([FromBody] RtmsRecord model)
        {
            long res = 0;
            if (model != null)
            {
                long lid = 0;
                if (long.TryParse(model.SID, out lid))
                {
                    model.ID = lid;
                    res = rtmsRecordRepository.Delete(model);
                }
            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }
    }
}
